package com.tigercel.xiangchengyun.ui.fragments

import android.content.Intent
import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.lzy.okgo.OkGo
import com.lzy.okgo.callback.StringCallback
import com.lzy.okgo.model.Response
import com.tigercel.xiangchengyun.R
import com.tigercel.xiangchengyun.adapter.NewsPagerAdapter
import com.tigercel.xiangchengyun.ui.activities.LoginActivity
import com.tigercel.xiangchengyun.ui.activities.ReportNewsActivity
import com.tigercel.xiangchengyun.utils.Global
import com.tigercel.xiangchengyun.utils.JsonUtil
import com.tigercel.xiangchengyun.utils.PreferencesUtil
import com.tigercel.xiangchengyun.utils.TabLayoutUtil
import kotlinx.android.synthetic.main.fragment_report_material.*
import kotlinx.android.synthetic.main.include_titlebar.*
import org.jetbrains.anko.support.v4.toast

/**
 * 民生界面
 */
class ReportMaterialFragment : Fragment() {

    private var containerView: View? = null
    val titles = mutableListOf<String>()
    private var fragments: MutableList<Fragment>? = null
    private var localDatas: PreferencesUtil?=null
    val channelids = mutableListOf<String>()
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        containerView = inflater.inflate(R.layout.fragment_report_material, container, false)
        return containerView
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        titlebar_tv.text = "便民服务"
        titlebar_iv_right.setImageResource(R.drawable.btn_nav_myreport)
        titlebar_iv_right.setOnClickListener({ toast("分享") })
        titlebar_iv_right.visibility = View.GONE



//        TitleBuilder(this!!.containerView!!)
//                .setLeftImage(R.mipmap.ic_launcher)
//                .setLeftOnClickListener(View.OnClickListener { toast("24") })

//        initNewsChannels()
//        initFragments()
        getChannels()
    }

//    private fun initNewsChannels() {
//        titles = listOf("最新", "民生", "其他", "FM936")
//    }

    private fun initFragments() {
        fragments = ArrayList()
        for (i in 0 until channelids!!.size) {
            var fragment: Fragment = NewsReportFragment.newInstance(channelids[i].toInt())!!
            (fragments as ArrayList<Fragment>).add(fragment)
        }
    }

    private fun getChannels() {
        OkGo.get<String>(Global.baseUrl + "v1/channel/format")
                .tag(this)
                .params("type", "3")
                .execute(object : StringCallback() {
                    override fun onSuccess(response: Response<String>?) {
                        var content: String? = response!!.body()
                        val status = JsonUtil.get_key_string("status", content!!)
                        val msg = JsonUtil.get_key_string("msg", content!!)
                        val code = JsonUtil.get_key_string("code", content!!)
                        if (status == "0") {

                            val data = JsonUtil.getList("data", content!!)
//                            titles.add(getString(R.string.recommend))
                            for (i in 0 until data.size) {
                                var chanle_name = JsonUtil.get_key_string("name", data[i])
                                var chanle_id = JsonUtil.get_key_string("channel_id", data[i])
                                titles.add(chanle_name)
                                channelids.add(chanle_id)
                            }
//                            initNewsChannels()
                            initFragments()
                            viewPager.adapter = NewsPagerAdapter(childFragmentManager, titles, fragments)
                            tabLayout.setupWithViewPager(viewPager)
                            TabLayoutUtil.dynamicSetTabLayoutMode(tabLayout)
                        } else {
                            toast(getString(R.string.readFaile))
                        }
                    }
                })
    }

    override fun onResume() {
        super.onResume()
        localDatas = PreferencesUtil(activity)
        var isAlreadyLogin = localDatas!!.preferences["isAlreadyLogin"]
        fab.setOnClickListener({
            if (isAlreadyLogin =="true"){
                startActivity(Intent(activity, ReportNewsActivity::class.java))
            }else{
                startActivity(Intent(activity, LoginActivity::class.java))
            }

        })
    }
}
